package com.exam.controller;

import com.exam.entity.*;
import com.exam.entity.common.ResponseResult;
import com.exam.service.*;
import com.exam.util.JsonUtils;
import com.exam.util.TimeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
public class ErrorQuestionController {

    @Autowired
    @Qualifier("errorQuestionServiceImpl")
    private ErrorQuestionService errorQuestionService;
    ResponseResult responseResult = new ResponseResult();

    @Autowired
    @Qualifier("singleChoiceQServiceImpl")
    private SingleChoiceQService singleChoiceQService;
    @Autowired
    @Qualifier("judgmentQServiceImpl")
    private JudgmentQService judgmentQService;

    @Autowired
    @Qualifier("shortAnswerQServiceImpl")
    private ShortAnswerQService shortAnswerQService;

    @Autowired
    @Qualifier("fillBlanksQServiceImpl")
    private FillBlanksQService fillBlanksQService;


    @RequestMapping("/add_error_question")
    @ResponseBody
    public String add_error_question(ErrorQuestion errorQuestion) {
        int question = errorQuestionService.get_error_question(errorQuestion);
        if (question == 1) {
            responseResult.setCode(200);
            responseResult.setMessage("该题已在错题集");
        } else {
            errorQuestion.setEq_creat_time(TimeUtils.getTime());
            int i = errorQuestionService.add_error_question(errorQuestion);
            if (i == 1) {
                responseResult.setCode(200);
                responseResult.setMessage("添加成功");
            } else {
                responseResult.setCode(0);
                responseResult.setMessage("添加失败");
            }
        }
        return JsonUtils.getJson(responseResult);
    }

    @RequestMapping("/get_error_question_by_u_id")
    @ResponseBody
    public String get_error_question_by_u_id(int u_id) {
        List<ErrorQuestion> errorQuestionByUId = errorQuestionService.get_error_question_by_u_id(u_id);
        for (ErrorQuestion errorQuestion : errorQuestionByUId) {
            String q_type = errorQuestion.getQ_type();
            int q_id = errorQuestion.getQ_id();
            if (q_type.equals("单选")) {
                SingleChoiceQuestions scqByCId = singleChoiceQService.get_scq_by_c_id(q_id);
                errorQuestion.setObjects(scqByCId);
            } else if (q_type.equals("判断")) {
                JudgmentQuestions jqByJId = judgmentQService.get_jq_by_j_id(q_id);
                errorQuestion.setObjects(jqByJId);
            } else if (q_type.equals("简答")) {
                ShortAnswerQuestions saqBySaId = shortAnswerQService.get_saq_by_sa_id(q_id);
                errorQuestion.setObjects(saqBySaId);
            }else if (q_type.equals("填空")) {
                FillBlanksQuestions fbqByFbId = fillBlanksQService.get_fbq_by_fb_id(q_id);
                errorQuestion.setObjects(fbqByFbId);
            } else {
                continue;
            }
        }
        responseResult.setCode(200);
        responseResult.setMessage("成功");
        responseResult.setResult(errorQuestionByUId);
        System.out.println(JsonUtils.getJson(responseResult));
        return JsonUtils.getJson(responseResult);
    }

    @RequestMapping("/del_error_question_by_eq_id")
    @ResponseBody
    public String del_error_question_by_eq_id(int eq_id) {
        int i = errorQuestionService.del_error_question_by_eq_id(eq_id);

        if (i == 1) {
            responseResult.setCode(200);
            responseResult.setMessage("移除成功");
        } else {
            responseResult.setCode(0);
            responseResult.setMessage("移除失败");
        }

        return JsonUtils.getJson(responseResult);
    }
}

